package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.math.BigDecimal;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.tool.image.ImageMixDeal;
import com.updateNew.MyIsif;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.ZhuFrame;
import org.come.model.Achievement;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.ScrenceUntil;
import org.come.until.StringUtil;

import com.tool.role.RoleData;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import org.come.until.Util;

/**
 * 功绩千秋
 * @author admin
 *
 */
public class SpotAchievemShowJpanel extends JPanel {
    private Achievement achievement;
    private JLabel headLab;
    private JLabel priceLab;
    private JLabel lbLab;
    private JLabel jlTextLab;
    private JLabel smTextLab;
    private JLabel taskNameLab;
//    private JLabel imgLab;
    private int type;
    private int index;
    
    private ImageIcon pslb = CutButtonImage.getImage("inkImg/hongmu/pslb.png", 25, 25);
    
    private ImageIcon pslbg = CutButtonImage.getImage("inkImg/hongmu/pslbg.png", 25, 25);
    
//    private ImageIcon imgsd = CutButtonImage.getImage("inkImg/background/S192.png", 56, 50);
    private static Sprite triple = SpriteFactory.VloadSprite("resource/mouse/BJF (23).tcp", null);//特效路径
    private ImageIcon imgwc = CutButtonImage.getImage("inkImg/hongmu/完成.png", 53, 42);
    
    public String cjjf = "0"; 
    
    
    public SpotAchievemShowJpanel(Achievement achievement, int index, int type) {
        this.setLayout(null);
        this.setOpaque(false);
        int numty = Util.random.nextInt(41);
        triple = SpriteFactory.VloadSprite("resource/mouse/BJF ("+ numty +").tcp", null);
        headLab = new JLabel();
        priceLab = new JLabel();
        lbLab = new JLabel();
        jlTextLab = new JLabel();
        smTextLab = new JLabel();
        taskNameLab = new JLabel();
//        imgLab = new JLabel();
        this.add(priceLab);
        this.add(lbLab);
        this.add(jlTextLab);
        this.add(smTextLab);
        this.add(taskNameLab);
        init(index, type);
        this.add(headLab);

//        this.add(imgLab);
        setAchievementBean(achievement);
        this.type = type;
        this.index = index;
    }

    private void init(int index, int type) {
        this.setPreferredSize(new Dimension(348, 83));
        this.setBounds(0, 0 + 83 * index, 348 , 83);
        if(MyIsif.getStyle().equals("水墨UI")){
        backIcon = new ImageIcon("inkImg/background/gjxx.png");
        }else{
        backIcon = new ImageIcon("inkImg/hongmu/gjxx.png");
        headLab.setBounds(6 + 2, 6 + 2, 40, 40);
    }
    }
    public void setAchievementBean(Achievement achievement) {
    	cjjf = RoleData.getRoleData().getLoginResult().getAchieveRecordtype(achievement.getConditions());
        this.achievement = achievement;
        priceLab.setBounds(300, 10, 100, 30);
        priceLab.setFont(new Font("楷体", 0, 16));
        priceLab.setForeground(Color.white);
        priceLab.setText(achievement.getPrice());
        taskNameLab.setBounds(80, 15, 200, 20);
        taskNameLab.setFont(new Font("楷体", 1, 18));
        if(MyIsif.getStyle().equals("水墨UI")){
            priceLab.setForeground(Color.white);
            taskNameLab.setForeground(Color.BLACK);
        }else{
            priceLab.setForeground(Color.white);
            taskNameLab.setForeground(UIUtils.COLOR_BTNPUTONG2[0]);
        }
        taskNameLab.setText(achievement.getTaskName());
        smTextLab.setBounds(80, 45, 300, 20);
        smTextLab.setFont(new Font("楷体", 1, 16));
        smTextLab.setForeground(Color.gray);
        smTextLab.setText(achievement.getConditions());
        jlTextLab.setBounds(20, 82, 300, 20);
        jlTextLab.setFont(new Font("楷体", 1, 16));
        jlTextLab.setForeground(Color.RED);
        jlTextLab.setText(achievement.getDescribeText());
        jlTextLab.setVisible(false);
        if(StringUtils.isNotEmpty(achievement.getGoodsId())) {
        	if(cjjf.equals("-1")) {
        		lbLab.setIcon(pslbg);
        	}else {
        		lbLab.setIcon(pslb);
        	}
        	lbLab.setBounds(taskNameLab.getWidth(), 10, 25, 25);
        	lbLab.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                	if(!cjjf.equals("-1") && Integer.parseInt(cjjf) == Integer.parseInt(achievement.getNum())) {
                		if (e.getClickCount() == 1) { // 单击事件
                			String sendmes = Agreement.getAgreement().roleAchieveAgreement("领取奖励="+achievement.getConditions());
                			SendMessageUntil.toServer(sendmes);
                			lbLab.setIcon(pslbg);
                		}
                	}else{
                        ZhuFrame.getZhuJpanel().addPrompt("未满足领取条件！");
                    }
                }
            });
        }else {
        	lbLab.setIcon(null);
        }
//        imgLab.setIcon(imgsd);
//        imgLab.setBounds(15, 15, 56, 50);
    }

    private ImageIcon icon;
    private ImageIcon backIcon;
    private boolean isSelect; // 是否选中
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(MyIsif.getStyle().equals("水墨UI")){
            if (backIcon != null) {
                g.drawImage(backIcon.getImage(), 2, 2, this);
            }
            triple.updateToTime(ImageMixDeal.userimg.getTime(), 0);
            triple.draw(g, 43, 69);

            if (isSelect) {
                backIcon = new ImageIcon("inkImg/background/gjxx1.png");
//        	this.setBounds(0, 0 + 83 * index, 348 , 113);
                paintCommodityBorder((Graphics2D) g, Color.GREEN, getWidth() - 1, getHeight() - 1);
                jlTextLab.setVisible(true);
            }else {
//        	this.setBounds(0, 0 + 83 * index, 348 , 83);
                backIcon = new ImageIcon("inkImg/background/gjxx.png");
                jlTextLab.setVisible(false);
            }
        }else{
            if (backIcon != null) {
                g.drawImage(backIcon.getImage(), 2, 2, this);
            }
            //动画 不动ImageMixDeal.userimg.getTime()改成3000
            triple.updateToTime(ImageMixDeal.userimg.getTime(), 1);
            triple.draw(g, 43, 69);

            if (isSelect) {
                backIcon = new ImageIcon("inkImg/hongmu/gjxx1.png");
//        	this.setBounds(0, 0 + 83 * index, 348 , 113);
                paintCommodityBorder((Graphics2D) g, Color.GREEN, getWidth() - 1, getHeight() - 1);
                jlTextLab.setVisible(true);
            }else {
//        	this.setBounds(0, 0 + 83 * index, 348 , 83);
                backIcon = new ImageIcon("inkImg/hongmu/gjxx.png");
                jlTextLab.setVisible(false);
            }
        }
        cjjf = RoleData.getRoleData().getLoginResult().getAchieveRecordtype(achievement.getConditions());
        if((StringUtils.isNotEmpty(cjjf) && Integer.parseInt(cjjf)>=Integer.parseInt(achievement.getNum())) || cjjf.equals("-1")) {
        	g.drawImage(imgwc.getImage(), 220, 15, this);
        }else {
        	g.setFont(new Font("楷体", 1, 14));
            if(!MyIsif.getStyle().equals("水墨UI")){
                g.setColor(UIUtils.COLOR_BTNPUTONG2[0]);
            }else{
                g.setColor(Color.black);
            }
        	g.drawString("完成进度："+(cjjf.equals("")?0:cjjf)+"/"+achievement.getNum(), 210, 70);
        }
    } 

    protected void paintCommodityBorder(Graphics2D g2, Color color, int width, int height) {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(color);
        // 绘制圆角矩形边框
        RoundRectangle2D borderShape = new RoundRectangle2D.Float(0, 0, width, height, 10, 10);
        g2.draw(borderShape);
    }

    public JLabel getHeadLab() {
        return headLab;
    }
    
    public void setSelect(boolean select) {
        isSelect = select;
    }

    public boolean isSelect() {
        return isSelect;
    }

	public Achievement getAchievement() {
		return achievement;
	}

	public void setAchievement(Achievement achievement) {
		this.achievement = achievement;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public ImageIcon getIcon() {
		return icon;
	}

	public void setIcon(ImageIcon icon) {
		this.icon = icon;
	}

	public ImageIcon getBackIcon() {
		return backIcon;
	}

	public void setBackIcon(ImageIcon backIcon) {
		this.backIcon = backIcon;
	}

	public void setHeadLab(JLabel headLab) {
		this.headLab = headLab;
	}
	
}
